//
//  BYLocationViewController.m
//  School+
//
//  Created by Sean Sebastian on 16/5/7.
//  Copyright © 2016年 Sean Sebastian. All rights reserved.
//

#import "BYLocationViewController.h"

#import "UIScrollView+UITouch.h"
#import "UIView+Extension.h"

@interface BYLocationViewController () <UIAlertViewDelegate>

@end

@implementation BYLocationViewController


- (UIImageView *)imageView
{
    if (_imageView == nil) {
        UIImage *image = [UIImage imageNamed:@"school.jpg"];
        CGFloat width = image.size.width;
        CGFloat height = image.size.height;
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, width, height)];
        imageView.image = image;
        return imageView;
    }
    return _imageView;
}

- (UIScrollView *)scrollView
{
    if (_scrollView == nil) {
        UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
        scrollView.contentSize = self.imageView.frame.size;
        scrollView.showsHorizontalScrollIndicator = NO;
        scrollView.showsVerticalScrollIndicator = NO;
        _scrollView = scrollView;
        return scrollView;
    }
    return _scrollView;
}

- (UIButton *)pinButton
{
    if (_pinButton == nil) {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        [btn addTarget:self action:@selector(btnClick) forControlEvents:UIControlEventTouchUpInside];
        [btn setImage:[UIImage imageNamed:@"pin"] forState:UIControlStateNormal];
        _pinButton = btn;
        return btn;
    }
    return _pinButton;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithTitle:@"back" style:UIBarButtonItemStylePlain target:self action:@selector(backClick)];
    
    self.navigationItem.leftBarButtonItem =item;
    self.title = @"点击大头针添加位置";
    
    [self.view addSubview:self.scrollView];
    [self.scrollView addSubview:self.imageView];
    [self.scrollView addSubview:self.pinButton];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    
    NSSet *allTouches = [event allTouches];
    UITouch *touch = [allTouches anyObject];
    CGPoint point = [touch locationInView:[touch view]];
    int x = point.x;
    int y = point.y;
    
    int pinX = x-25;
    int pinY = y-47;
    
    CGRect temp = self.pinButton.frame;
    temp.origin.y = 0;
    temp.origin.x = pinX;
    self.pinButton.frame = temp;
    [self.view setNeedsLayout];
    
    [UIView animateWithDuration:1.0 animations:^{
        self.pinButton.frame = CGRectMake(pinX, pinY, 50, 50);
        [self.view setNeedsLayout];
    } completion:^(BOOL finished) {
        [UIView animateWithDuration:1.0 animations:^{
            CGRect temp = self.pinButton.frame;
            temp.origin.y = pinY + 30;
            temp.origin.x = pinX;
            self.pinButton.frame = temp;
            [self.view setNeedsLayout];
        } completion:^(BOOL finished) {
            [UIView animateWithDuration:0.3 animations:^{
                self.pinButton.frame = CGRectMake(pinX, pinY, 50, 50);
                [self.view setNeedsLayout];
            }];
            
        }];
    }];
    
    [BYLocation sharedLocation].locationX = pinX;
    [BYLocation sharedLocation].locationY = pinY;

}

- (void)backClick
{
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)btnClick
{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"添加位置" message:@"请输入详细位置地址" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"添加", nil];
    alert.alertViewStyle = UIAlertViewStylePlainTextInput;
    alert.delegate = self;
    
    [UIView animateWithDuration:0.5 animations:^{
        
        // 截图
        
        NSData *imageData = UIImageJPEGRepresentation([self.view capture], 1.0);
        [BYLocation sharedLocation].locationImageData = imageData;
        
    } completion:^(BOOL finished) {
        [alert show];
    }];
    
}

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    
    // 取消按钮是第0个，确定键是第1个
    if (buttonIndex == 1) {
        [BYLocation sharedLocation].locationDetail = [alertView textFieldAtIndex:0].text;
    }
    
    [self dismissViewControllerAnimated:YES completion:nil];
}

@end
